#include <iostream>
#include <fstream>

using namespace std;

#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;		
typedef int ElemType; 	

#define MAXSIZE 100  	
typedef struct{
	ElemType *elem;		
	int length;				
}SqList;

// 初始化顺序表
Status InitList(SqList &L) {
    L.elem = new ElemType[MAXSIZE];
    if (!L.elem) exit(OVERFLOW);
    L.length = 0;
    return OK;
}

// 销毁顺序表
void DestroyList(SqList &L) {
    if (L.elem) delete[] L.elem;
    L.elem = NULL;
    L.length = 0;
}

// 清空顺序表
Status ClearList(SqList &L) {
    L.length = 0;
    return OK;
}

// 判断顺序表是否为空
Status ListEmpty(SqList L) {
    return L.length == 0 ? OK : ERROR;
}

// 获取顺序表长度
int ListLength(SqList L) {
    return L.length;
}

// 按位序获取元素
Status GetElem(SqList L, int i, ElemType &e) {
    if (i < 1 || i > L.length) return ERROR;
    e = L.elem[i - 1];
    return OK;
}

// 按值查找位序
int LocateElem(SqList L, ElemType e) {
    for (int i = 0; i < L.length; i++) {
        if (L.elem[i] == e) return i + 1;
    }
    return 0;
}

// 插入元素
Status ListInsert(SqList &L, int i, ElemType e) {
    if (i < 1 || i > L.length + 1) return ERROR;
    if (L.length == MAXSIZE) return ERROR;
    for (int j = L.length-1; j >= i-1; j--) {
	    L.elem[j+1] = L.elem[j];
	}
    L.elem[i - 1] = e;
    L.length++;
    return OK;
}

// 删除元素
Status ListDelete(SqList &L, int i, ElemType &e) {
    if (i < 1 || i > L.length) return ERROR;
    e = L.elem[i - 1];
    for (int j = i; j < L.length; j++) {
        L.elem[j - 1] = L.elem[j];
    }
    L.length--;
    return OK;
}

// 打印顺序表
void PrintList(SqList L) {
    if (ListEmpty(L) == OK) {
        cout << "顺序表为空" << endl;
        return;
    }
    cout << "顺序表元素: ";
    for (int i = 0; i < L.length; i++) {
        cout << L.elem[i] << " ";
    }
    cout << endl;
}

// 从文件导入数据创建顺序表
Status ImportListFromFile(SqList &L, const string &filename) {
    ifstream file(filename);
    if (!file.is_open()) {
        cout << "无法打开文件: " << filename << endl;
        return ERROR;
    }
    
    // 清空当前顺序表
    ClearList(L);
    
    ElemType value;
    while (file >> value && L.length < MAXSIZE) {
        L.elem[L.length] = value;
        L.length++;
    }
    
    file.close();
    cout << "成功从文件导入 " << L.length << " 个元素" << endl;
    return OK;
}

// 保存顺序表到文件
Status ExportListToFile(SqList L, const string &filename) {
    ofstream file(filename);
    if (!file.is_open()) {
        cout << "无法创建文件: " << filename << endl;
        return ERROR;
    }
    
    for (int i = 0; i < L.length; i++) {
        file << L.elem[i] << " ";
    }
    
    file.close();
    cout << "成功导出 " << L.length << " 个元素到文件: " << filename << endl;
    return OK;
}

// 显示菜单
void ShowMenu() {
    cout << "\n======= 顺序表操作菜单 =======" << endl;
    cout << "1. 初始化顺序表" << endl;
    cout << "2. 销毁顺序表" << endl;
    cout << "3. 清空顺序表" << endl;
    cout << "4. 判断顺序表是否为空" << endl;
    cout << "5. 获取顺序表长度" << endl;
    cout << "6. 按位序获取元素" << endl;
    cout << "7. 按值查找位序" << endl;
    cout << "8. 插入元素" << endl;
    cout << "9. 删除元素" << endl;
    cout << "10. 打印顺序表" << endl;
    cout << "11. 从文件导入数据" << endl;
    cout << "12. 保存数据到文件" << endl;
    cout << "0. 退出程序" << endl;
    cout << "===============================" << endl;
    cout << "请选择操作: ";
}

int main() {
    SqList L;
    int choice, position;
    ElemType value;
    Status result;
    string filename;
    
    // 初始时顺序表未初始化
    L.elem = NULL;
    L.length = 0;
    
    do {
        ShowMenu();
        cin >> choice;
        
        switch (choice) {
            case 1:
                if (L.elem != NULL) {
                    cout << "顺序表已存在，请先销毁!" << endl;
                } else {
                    result = InitList(L);
                    if (result == OK) 
                        cout << "顺序表初始化成功!" << endl;
                    else 
                        cout << "顺序表初始化失败!" << endl;
                }
                break;
                
            case 2:
                if (L.elem == NULL) {
                    cout << "顺序表未初始化!" << endl;
                } else {
                    DestroyList(L);
                    cout << "顺序表已销毁!" << endl;
                }
                break;
                
            case 3:
                if (L.elem == NULL) {
                    cout << "顺序表未初始化!" << endl;
                } else {
                    ClearList(L);
                    cout << "顺序表已清空!" << endl;
                }
                break;
                
            case 4:
                if (L.elem == NULL) {
                    cout << "顺序表未初始化!" << endl;
                } else {
                    result = ListEmpty(L);
                    if (result == OK) 
                        cout << "顺序表为空!" << endl;
                    else 
                        cout << "顺序表不为空!" << endl;
                }
                break;
                
            case 5:
                if (L.elem == NULL) {
                    cout << "顺序表未初始化!" << endl;
                } else {
                    cout << "顺序表长度为: " << ListLength(L) << endl;
                }
                break;
                
            case 6:
                if (L.elem == NULL) {
                    cout << "顺序表未初始化!" << endl;
                } else {
                    cout << "请输入要获取的元素位置: ";
                    cin >> position;
                    result = GetElem(L, position, value);
                    if (result == OK) 
                        cout << "第" << position << "个元素是: " << value << endl;
                    else 
                        cout << "位置不合法!" << endl;
                }
                break;
                
            case 7:
                if (L.elem == NULL) {
                    cout << "顺序表未初始化!" << endl;
                } else {
                    cout << "请输入要查找的元素值: ";
                    cin >> value;
                    position = LocateElem(L, value);
                    if (position != 0) 
                        cout << "元素" << value << "位于第" << position << "个位置" << endl;
                    else 
                        cout << "未找到元素" << value << endl;
                }
                break;
                
            case 8:
                if (L.elem == NULL) {
                    cout << "顺序表未初始化!" << endl;
                } else {
                    cout << "请输入要插入的位置和值(位置 值): ";
                    cin >> position >> value;
                    result = ListInsert(L, position, value);
                    if (result == OK) 
                        cout << "插入成功!" << endl;
                    else 
                        cout << "插入失败!位置不合法或顺序表已满" << endl;
                }
                break;
                
            case 9:
                if (L.elem == NULL) {
                    cout << "顺序表未初始化!" << endl;
                } else {
                    cout << "请输入要删除的位置: ";
                    cin >> position;
                    result = ListDelete(L, position, value);
                    if (result == OK) 
                        cout << "删除成功!被删除的元素是: " << value << endl;
                    else 
                        cout << "删除失败!位置不合法" << endl;
                }
                break;
                
            case 10:
                if (L.elem == NULL) {
                    cout << "顺序表未初始化!" << endl;
                } else {
                    PrintList(L);
                }
                break;
                
            case 11:
                if (L.elem == NULL) {
                    cout << "顺序表未初始化，请先初始化!" << endl;
                } else {
                    cout << "请输入要导入的文件名: ";
                    cin >> filename;
                    result = ImportListFromFile(L, filename);
                    if (result == ERROR) {
                        cout << "导入文件失败!" << endl;
                    }
                }
                break;
                
            case 12:
                if (L.elem == NULL) {
                    cout << "顺序表未初始化!" << endl;
                } else if (ListEmpty(L) == OK) {
                    cout << "顺序表为空，无法导出!" << endl;
                } else {
                    cout << "请输入要保存的文件名: ";
                    cin >> filename;
                    result = ExportListToFile(L, filename);
                    if (result == ERROR) {
                        cout << "导出文件失败!" << endl;
                    }
                }
                break;
                
            case 0:
                if (L.elem != NULL) {
                    DestroyList(L);
                }
                cout << "程序已退出!" << endl;
                break;
                
            default:
                cout << "无效的选择，请重新输入!" << endl;
        }
    } while (choice != 0);
    
    return 0;
}
